<?php
defined('IN_TS') or die('Access Denied.');
 
//用户是否登录
$userid = aac('user')->isLogin();

switch($ts){
	//发送消息页面
	case "add":
		
		$touserid = intval($_GET['touserid']);
		
		if($userid == $touserid || !$touserid) tsNotice("Sorry！自己不能给自己发送消息的！& 对方为空!");
		
		$strUser = $new['user']->getOneUser($userid);
		
		$strTouser = $new['user']->getOneUser($touserid);

		if(!$strTouser) tsNotice("Sorry！对方不存在!");
		$title = "发送短消息";
		include template("message_add");
		break;
	
	case "do":

		$msg_userid = $userid;
		$msg_touserid = intval($_POST['touserid']);
		$msg_content = trim($_POST['content']);
		
		aac('system')->antiWord($msg_content);

		aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content);

		header("Location: ".tsUrl('message','my'));
		
		break;

	//发送贺卡页面
	case "addcard":

		$touserid = intval($_GET['touserid']);
		$content = trim($_POST['content']);
		$tplid = intval($_POST['tplid']);
		$sendtime = intval($_POST['sendtime']);

		if($sendtime) {
			$sendtime = date('Y-m-d', $sendtime);
		}

		if($userid == $touserid || !$touserid) tsNotice("Sorry！自己不能给自己发送贺卡！& 对方为空!");

		$strUser = $new['user']->getOneUser($userid);

		$strTouser = $new['user']->getOneUser($touserid);

		if(!$strTouser) tsNotice("Sorry！对方不存在!");

		$title = "发送贺卡";
		include template("message_addcard");
		break;

	//贺卡预览页面
	case "cardpreview":

		$touserid = intval($_POST['touserid']);
		$content = trim($_POST['content']);
		$tplid = intval($_POST['tplid']);
		$sendtime = $_POST['sendtime'];


		$sendtime = trim(str_replace('AM','',$sendtime));
		$sendtime = trim(str_replace('PM','',$sendtime));
		$sendtime = strtotime($sendtime);


		$toUser = aac('user')->find('user_info',array('userid'=>$touserid));

		$title = '贺卡预览';
		include template("cardpreview");
		break;

	//发送贺卡
	case "sendcard":

		$touserid = intval($_POST['touserid']);
		$content = trim($_POST['content']);
		$tplid = intval($_POST['tplid']);
		$sendtime = intval($_POST['sendtime']);


		if($touserid==''){
			tsNotice('收卡人不能为空！');
		}

		if($content==''){
			tsNotice('内容不能为空！');
		}

		if($tplid==''){
			tsNotice('请选择模板！');
		}

		if($sendtime==''){
			tsNotice('发卡时间不能为空！');
		}

		aac('system')->antiWord($content);

		$cardid = aac('user')->create('user_card',array(
			'userid'=>$userid,
			'touserid'=>$touserid,
			'content'=>$content,
			'tplid'=>$tplid,
			'addtime'=>time(),
			'sendtime'=>$sendtime

		));

		$arrUser = aac('user')->find('user_info',array('userid'=>$touserid));
		//发一条广播
		$content = '，你有一张来自馆友的<a href="'.tsUrl('article','show',array('id'=>455)).'">贺卡，即将于'.date('Y-m-d',$sendtime).'收到！想知道怎样发贺卡的馆友，点击这条消息。</a>';
		aac('weibo')->create('weibo',array(
			'userid'=>$touserid,
			'content'=>$content,
			'isaudit'=>0,
			'addtime'=>date('Y-m-d H:i:s'),
			'uptime'=>date('Y-m-d H:i:s'),
		));

		header("Location: ".tsUrl('user','cardlist',array('first'=>1)));


		break;

	//删除贺卡
	case 'delcard':
		$cardid = intval($_POST['cardid']);
		$strCard = aac('user')->find('user_card',array('cardid'=>$cardid));

		if($strCard['status']==1&&$userid!=$strCard['touserid']||$strCard['status']==0&&$userid!=$strCard['userid']){
			tsNotice('没有操作权限！');
		}

		aac('user')->delete('user_card',array('cardid'=>$cardid));

		echo'1';
		break;
}